<?php
// Connect to Content Manager
require_once("includes/CMBGenericWebService.php");
$service = new CMBGenericWebService("db28cm", "cmjphp", "php1read");

// searching
if(($_POST['action']) == "search") {
	try {
		$value = strtolower($_POST['value']);
        $type = $_POST['type'];
        $multiple_keywords = false;
        // space delimiter
        if(strpos($value, " ")) {
        	$value = explode(" ", $value);
            $multiple_keywords = true;
        }
        // comma delimiter
        else if(strpos($value, ",")) {
        	$value = explode(",", $value);
            $multiple_keywords = true;
        }
        else {
        	$value = trim($value);
       	}
        if(empty($type)) {
        	$type = "Keyword";
        }
		if(!empty($value)) {
        	if($multiple_keywords) {
           		$search = "/*[(";
            	for($i=0; $i < sizeof($value); $i++) {
                        $search .= "@" . $type . " LIKE \"%" . $value[$i] . "%\"";
                        if($i < sizeof($value) - 1) {
                        	$search .= " OR ";
                        }
                }
                $search .= ")]";
           	}
            else {
				$search = "/*[.//@" . $type . " LIKE \"%" . $value . "%\"]";
            }
            //echo $search;
            $results = $_POST['results'];
			$result = $service->search($search, $results);
			$xml = simplexml_load_string($result->processXMLRequestReturn->xmlResponseText);
			unset($result);
			if($xml->ResultSet->attributes()->count > 0){
            ?>
                <table style="margin: 25px; background-color: #f0f0f0; padding: 15px;" cellpadding="6">
                	<tr>
                        <th>&nbsp;</th>
                      	<th>&nbsp;</th>
                        <th>Filename</th>
                		<th>Photographer</th>
                		<th>Department</th>
                		<?php /* ?><th>Keywords</th><?php */ ?>
                		<th>Date</th>
                		<th>Rating</th>
                	</tr>
                <?php
                foreach( $xml->ResultSet->Item as $item ){
                    $items[] = $item;
                }
                unset($xml);
                $i = 0;
                foreach($items as $item){
                    $URI = $item->attributes()->URI;
                    $attrs = $item->children()->children()->attributes();
                    
                    $file_name = str_replace(".jpg", "-thumbnail.jpg", $attrs["Image"]);
                ?>
                    <tr style="vertical-align: middle;">
                    	<td style="font-size: 13px; width: 150px;">
                        	<input type="button" value="download" onclick="window.location.href='download.html?image=<?php echo urlencode($attrs["Image"]); ?>&amp;URI=<?php echo urlencode($URI); ?>'" style="font-size: 14px; padding: 5px;" />
                        </td>
                        <td><a class="photo" href="photo.html?URI=<?php echo urlencode($URI); ?>"><img src="thumbnails/<?php echo $file_name; ?>" alt="thumbnail" /></a></td>
                        <td><?php echo $attrs["Image"]; ?></td>
                    	<td><?php echo ucwords($attrs["Author01"]); ?></td>
                    	<td><?php echo ucwords($attrs["Department"]); ?></td>
                    	<?php /* ?><td style="width: 200px;"><?php echo $attrs["Keyword"]; ?></td><?php */ ?>
                    	<td><?php echo $attrs["LIBDate"]; ?></td>
                    	<td><?php echo $attrs["Rating"]; ?></td>
                    </tr>
                <?php
                }
                ?>
            	</table>
            <?php
            }
            else {
            	echo "<h2>No Results Found</h2>";
            }
        }
        else {
        	if(!empty($_POST['value'])) {
            		echo "<h2>No Results Found</h2>";
            }
        }
    }
    catch( SoapFault $fault) {
    	echo ("SOAP Fault: (faultcode: {$fault->faultcode}\n" . "faultstring: {$fault->faultstring})");
    }
}
?>

<script type="text/javascript">
jQuery(document).ready(function() {					
	jQuery(".photo").fancybox({
				'width'				: '90%',
				'height'			: '90%',
				'autoScale'			: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'type'				: 'iframe'
			});
});
</script>
